﻿using System.Data;
using JSon = System.Text.Json.JsonSerializer;

namespace Lib.Serialization;
/// <summary>
/// 
/// </summary>
public static partial class JavaScript {
    
    
    #region "DataTable、DataSet 对象序列化"

    /// <summary>
    /// 序列化数据表
    /// </summary>
    /// <para>1.自定义输出名称："UID,Name:FullName,Password,Sex,Age"，格式如：字段原名:别名;</para>
    /// <para>2.要忽略的字段列："@,Password,Age"</para>
    public static string Serialize(this DataTable dt, string Properties = null) {
        if (dt == null) {
            return null;
        }
        try {
            return JSon.Serialize(dt.DataTableToList(Properties), JavaScriptOptions);
        } catch {
            return default;
        }
    }
    /// <summary>
    /// 序列化数据集
    /// </summary>
    public static string Serialize(this DataSet Object) {
        if (Object == null) {
            return null;
        }
        try {
            return JSon.Serialize(Object.DataSetToDictionary());
        } catch {
            return default;
        }
    }

    /// <summary>
    /// 数据表转键值对集合
    /// </summary>
    private static List<Dictionary<string, object>> DataTableToList(this DataTable dt, string Properties = null) {
        var Rows = new List<Dictionary<string, object>>();
        var Columns = dt.Columns;
        if (!string.IsNullOrEmpty(Properties)) {
            var Ignore = false;
            if (Properties.StartsWith('@')) {
                Ignore = true;
            }
            var Propertys = Properties.Split(',').ToList();
            if (Ignore) {
                Propertys.RemoveAt(0);
                foreach (var Key in Propertys) {
                    if (Columns.Contains(Key)) {
                        Columns.Remove(Key);
                    }
                }
            } else {
                Columns = new DataTable().Columns;
                string Key;
                string Alias;
                foreach (var Property in Propertys) {
                    Key = Property; Alias = null;
                    if (Property.Contains(':')) {
                        Key = Property.Split(":")[0];
                        Alias = Property.Split(":")[1];
                    }
                    if (dt.Columns.Contains(Key)) {
                        var Column = dt.Columns[Key];
                        if (Alias != null) {
                            Column.ColumnName = Alias;
                        }
                        Columns.Add(Column);
                    }
                }
            }

        }
        foreach (DataRow Rs in dt.Rows) {
            var Row = new Dictionary<string, object>();
            foreach (DataColumn e in Columns) {
                Row.Add(e.ColumnName, Rs[e.ColumnName] == DBNull.Value ? null : Rs[e.ColumnName]);
            }
            Rows.Add(Row);
        }
        return Rows;
    }
    /// <summary> 
    /// 数据集转键值对数组字典 
    /// </summary> 
    private static Dictionary<string, List<Dictionary<string, object>>> DataSetToDictionary(this DataSet Object) {
        var Result = new Dictionary<string, List<Dictionary<string, object>>>();
        foreach (DataTable dt in Object.Tables) {
            Result.Add(dt.TableName, dt.DataTableToList());
        }
        return Result;
    }

    #endregion

}
